<?php
/**
 * Created by PhpStorm.
 * User: jiayanxiu
 * Date: 15/6/15
 * Time: 15:38
 * 心愿单控制器 此页面的请求是要求用户登陆的
 */
class WishApp extends MallbaseApp {

	/**
	 * 当前用户请求的方法
	 * @var string
	 * @author jyx
	 * @date 2015/07/30
	 */
	private $method = '';
	/**
	 * 当前用户登陆的id
	 * @var int
	 * @author jyx
	 * @date 2015/07/30
	 */
	private $user_id = 0;

	/**
	 * 构造函数
	 */
	public function __construct() {
		$this->method = strtoupper($_SERVER['REQUEST_METHOD']);
		$this->user_id = $this->checkLogin();
		if (!$this->user_id) {
			$this->ajax_return(ERROR_LOGIN, '请您登陆！');
		}
	}
	/**
	 * 心愿单列表
	 */
	public function index() {
		$action = '心愿单列表';
		setLogParameter($action);
		if ($this->method !== 'GET') {
			$this->ajax_return(FLAG_ERROR, '非法的请求！');
		}
		$offset  = intval($this->get_gp('offset', 'G'));
		$size    = intval($this->get_gp('size', 'G'));
		$size = $size>0?$size:10;
		$offset = $offset>0?$offset:0;

		$limit = " LIMIT {$offset},{$size} ";
		$model = m('wish');
		$condition = " user_id='{$this->user_id}' ";
		//数据处理
		$data = $model->applist($condition,$limit);

		$this->ajax_return(FLAG_OK,'',$data);

	}

	/**
	 * 心愿单详情
	 */
	public function detail() {
		$action = '心愿单详情';
		setLogParameter($action);
		if ($this->method !== 'GET') {
			$this->ajax_return(FLAG_ERROR, '非法的请求！');
		}

		$offset  = intval($this->get_gp('offset', 'G'));
		$size    = intval($this->get_gp('size', 'G'));
		$size = $size > 0 ? $size : 10;
		$offset = $offset > 0 ? $offset : 0;

		$wish_id = intval($this->get_gp('wish_id','G'));

		$model = m('wish');
		$data = $model->detail($this->user_id, $wish_id," limit {$offset},{$size} ");
		if($data === false)
		{
			$error = $model->_errors;
			$e = is_array($error)?array_shift($error):$error;
			$this->ajax_return(FLAG_ERROR,$e);
		}
		else
		{
			$this->ajax_return(FLAG_OK,'',$data);
		}
	}

	/**
	 * 添加心愿单
	 */
	public function add() {
		$action = '添加心愿单';
		setLogParameter($action);
		if ($this->method !== 'POST') {
			$this->ajax_return(FLAG_ERROR, '非法的请求！');
		}
		$uri = 'http://vpn-m.herxi.com/';
		$model = m('wish');
		$keys = array('title','goods_id','spec_id','wish_total_time');
		$data = array();
		foreach ($keys as $v)
		{
			$data[$v] = $this->get_gp($v,'P');
		}

		$now = time();
		$sql = "select wish_id,title from ".DB_PREFIX."wish where goods_id='{$data['goods_id']}' and user_id='{$this->user_id}' and spec_id='{$data['spec_id']}' and end_time>'{$now}' and flag='0' ";

		$row = $model->getRow($sql);
		if(!empty($row)){
			$url = $uri."index.php?c=wish&a=run&goods_id={$data['goods_id']}&wish_id={$row['wish_id']}";
			$this->ajax_return(FLAG_ERROR,'已经存在',array('share_url'=>$url,'title'=>$row['title']));
		}

		$f = $model->add($data,$this->user_id);
		if($f)
		{
			$url = $uri."index.php?c=wish&a=run&goods_id={$data['goods_id']}&wish_id={$f}";
			$this->ajax_return(FLAG_OK,'添加成功',array('share_url'=>$url,'title'=>$data['title']));
		}
		else
		{
			$error = $model->_errors;
			$e = is_array($error)?array_shift($error):$error;

			$this->ajax_return(FLAG_ERROR,$e);
		}
	}

	/**
	 * 结束心愿单
	 * @author jyx
	 * @date 2015/07/30
	 */
	public function close() {
		$action = '结束心愿单';
		setLogParameter($action);
		if ($this->method !== 'POST') {
			$this->ajax_return(FLAG_ERROR, '非法的请求！');
		}
		$wish_id = intval($this->get_gp('wish_id','P'));
		$model = m('wish');
		$info = $model->infoOk($wish_id, false);
		if(empty($info))
		{
			$error = $model->_errors;
			$e = is_array($error)?array_shift($error):$error;
			$this->ajax_return(FLAG_ERROR,$e);
		}

		//要去掉其他的处理内容,现在可以删除处理了
		$addon_info = $model->moremoney($info);
		$flag = $addon_info['paymoney'] ? true : false;
		$f = $model->close($wish_id, $this->user_id, $flag);
		if($f)
		{
			$this->ajax_return(FLAG_OK,'操作成功');
		}
		else
		{
			$error = $model->_errors;
			$e = is_array($error)?array_shift($error):$error;
			$this->ajax_return(FLAG_ERROR,$e);
		}
	}


	/**
	 * 完成心愿单
	 * @author jyx dslfs
	 * @date 2015/07/30
	 */
	public function finish() {
		$action = '完成心愿单';
		setLogParameter($action);
		if ($this->method !== 'GET') {
			$this->ajax_return(FLAG_ERROR, '非法的请求！');
		}
		$wish_id = $this->get_gp('wish_id', 'G');
		$wish_id = $wish_id ? intval($wish_id) : 0;

		$model = m('wish');
		//查询出商品的总金额
		$sql = "SELECT w.wish_id,w.order_sn,gs.price FROM ".DB_PREFIX."order o LEFT JOIN ".DB_PREFIX."wish w
				ON o.order_sn=w.order_sn LEFT JOIN ".DB_PREFIX."goods_spec gs ON w.spec_id=gs.spec_id
				WHERE w.wish_id={$wish_id} AND w.user_id={$this->user_id} AND o.status<>".ORDER_CANCELED;
		$tmp = $model->getRow($sql);
        //如果存在订单号，说明已经生成订单了，则直接去付款
        if ($tmp['order_sn']) {
            $this->ajax_return(FLAG_OK, '此订单信息已经存在，请您去订单列表付款！', $tmp);
        }
		//查询出捐赠的总金额
		$sql = "SELECT SUM(donate_price) AS total_price FROM ".DB_PREFIX."wish_log WHERE wish_id={$wish_id} AND `status`=1";
		$temp = $model->getRow($sql);
		$price = floatval($tmp['price'] - $temp['total_price']);
		if ($price < 0 ) {
			$price = 0;
		}
		//如果金额相等，则去生成一条订单，入库
		if ($temp['total_price'] == $tmp['price'] || $price == 0) {
			$this->ajax_return(FLAG_ANOTHER, '不需要支付！');
		}
		$data['price'] = $price;
		$this->ajax_return(FLAG_OTHER, '支付余额！', $data);
	}




}